//
//  RegisterViewController.m
//  豆瓣
//
//  Created by liuxin on 15/10/26.
//  Copyright (c) 2015年 liuxin. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "User.h"
#import "UserInfoDB.h"
@interface RegisterViewController ()
@property(nonatomic,retain)UIAlertController *alert;
@property(nonatomic,retain)UIAlertController *password;
@property(nonatomic,retain)UIAlertController *duplicate;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"ddddddddddddddd");
    self.navigationItem.title = @"注册";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(Action)];
    
//    self.arrayPassword = [NSMutableArray array];
//    self.arrayUserName = [NSMutableArray array];
    
    RegisterView *registerV = [[RegisterView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    registerV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:registerV];
    [registerV release];
    self.alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"用户名或密码不能能为空,请认真填写" preferredStyle:UIAlertControllerStyleAlert];
    self.password =[UIAlertController alertControllerWithTitle:@"提示信息" message:@"两次输入的密码不相同,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
    self.duplicate =[UIAlertController alertControllerWithTitle:@"提示信息" message:@"这个用户名已经存在了,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        }];
    [self.duplicate addAction:ok];
    [self.password addAction:ok];
    [self.alert addAction:ok];
}
-(void)Action{
    [self saveNSUserDefaults];
    [self.navigationController popViewControllerAnimated:YES];
//    [self readNSUserDefaults];
}
-(void)saveNSUserDefaults{
    User *user = [User new];
    UITextField *userNameField = [self.view viewWithTag:1100];
    user.userName = userNameField.text;
    UITextField *passwordField = [self.view viewWithTag:1101];
    user.password = passwordField.text;
    UITextField *surePassField = [self.view viewWithTag:1102];
    user.surePassword = surePassField.text;
    UITextField *e_mailField = [self.view viewWithTag:1103];
    user.e_mail = e_mailField.text;
    UITextField *phoneField = [self.view viewWithTag:1104];
    user.phoneNumber = phoneField.text;
    
    if([user.userName isEqualToString:@""]||[user.password isEqualToString:@""]||[user.surePassword isEqualToString:@""]){
            [self presentViewController:self.alert animated:YES completion:^{
                userNameField.text = @"";
                surePassField.text = @"";
                passwordField.text = @"";
            }];
        }
    if(![user.password isEqualToString:user.surePassword]){
            [self presentViewController:self.password animated:YES completion:^{
                surePassField.text = @"";
                passwordField.text = @"";
            }];
        }
    
    [[UserInfoDB shareDataBase]openDB];
    [[UserInfoDB shareDataBase]createTable];
    NSArray *array = [[UserInfoDB shareDataBase]selectUser:user.userName];
    if(array.count != 0){
        [self presentViewController:self.duplicate animated:YES completion:^{
            userNameField.text = @"";
            surePassField.text = @"";
            passwordField.text = @"";
        }];
    }else{
    [[UserInfoDB shareDataBase]insertUser:user];
    }
}
//-(void)readNSUserDefaults{
//    NSString*string = [self.allInfo stringForKey:@"Username"];
//    NSLog(@"%@",string);
//    UITextField *passWord = [self.view viewWithTag:1101];
//    passWord.text = string;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
